#include "LevelTest.h"

LevelTest::LevelTest(LPDIRECT3DDEVICE9 d3ddev)
{
	this->d3ddev = d3ddev;
	levelResource = new Resource(d3ddev);
	levelResource->initPhysics(1000.0f, 1000.0f, -1000.0f);
	this->world_physics = levelResource->getWorldPhysics();
	camera = NULL;
}

LevelTest::~LevelTest()
{
	delete levelResource;

	if( shaders.size() != 0 )
		for(int i=0; i<(int)shaders.size(); i++)
			delete shaders[i];
	shaders.clear();
}

void LevelTest::SetLevelCamera(Camera *camera)
{
	/**
	Set a camera for the level
	*/

	if( camera != NULL )
		this->camera = camera;
	else
		_cprintf("No Camera\n");
}

void LevelTest::BuildLevel()
{
	/**
	Build the level; Load the level
	*/

	//Load Shaders

	ShaderEffect * shader = new ShaderEffect();
	shader->LoadEffect(d3ddev);

	shaders.push_back( shader );

	//Loads Level From Map Editor
	//Map::LoadMap(levelResource, "../../Levels/map1.xml", NONPHYS, shader); //uncomment to test
	//Map::LoadMap(levelResource, "../../Levels/SeansDemoLevel_1.xml", 10.0f, shader); //uncomment to test
	//Map::LoadMap(levelResource, "../../Levels/SeansDemoLevel_1_2.xml", 10.0f, shader);
	Map::LoadMap(levelResource, "../../Levels/SeansDemoLevel_1_3.xml", 10.0f, shader);
	//Map::LoadMap(levelResource, "../../Levels/Nothing2.xml", 2.0f, shader); //uncomment to test
	//Map::LoadMap(levelResource, "../../Levels/Simple.xml", 10.0f, shader);

	//Add Objects
	levelResource->AddObject("Ground");
	levelResource->AddObject("L_Wall");
	levelResource->AddObject("R_Wall");
	levelResource->AddObject("Ceiling");
	levelResource->AddObject("YarnBall");
	levelResource->AddObject("Platform_1");
	levelResource->AddObject("Platform_2");
	levelResource->AddObject("Platform_3");

	levelResource->AddObject("AnimTestObject");
	
	//////////
	//Bridge//
	//////////

	//You don't have to understand this. It was to make sure the box2d anchor stuff worked

	char buf[2];
	std::string nn;
	std::string bb[4];
	std::string bbp[2];

	for(int i=0; i<2; i++)
	{
		nn.clear();

		_itoa_s(i, buf, 10);

		std::string nn = "b";
		nn.append(buf);

		_cprintf("%s\n", nn.c_str());
		bbp[i] = nn.c_str();

		levelResource->AddObject(nn.c_str());
		levelResource->GetObject(nn.c_str())->setPosition(i*15.0f, 0.0f, 69.0f);
		levelResource->GetObject(nn.c_str())->setMass(0.0f);
		levelResource->GetObject(nn.c_str())->createPhysics(BOX, STATIC);

		nn.append(" ");

		if( i<1 )
		for(int j=0; j<4; j++)
		{
			_itoa_s(j, buf, 10);
			nn[ nn.length()-1 ] = *buf;

			_cprintf("%s\n", nn.c_str());

			bb[j] = nn.c_str();

			levelResource->AddObject(nn.c_str());
			levelResource->GetObject(nn.c_str())->setPosition(j*3.0f+3.0f, 0.0f, 69.0f);
			levelResource->GetObject(nn.c_str())->setMass(0.1f);
			levelResource->GetObject(nn.c_str())->createPhysics(BOX, DYNAMIC);


			if(j>0)
			{
				levelResource->GetObject(nn.c_str())->ConnectBody(levelResource->GetObject(bb[j-1].c_str()));
			}
			else if( j == 0)
			{
				levelResource->GetObject(bbp[0].c_str())->ConnectBody(levelResource->GetObject(nn.c_str()));
			}

		}

		if( i == 1 )
		{
			levelResource->GetObject(bb[3])->ConnectBody(levelResource->GetObject(bbp[1]));
		}
	}

	///////
	//END//
	///////

	levelResource->GetObject("Ground")->setPosition(0.0f,-30.0f,69.0f);
	levelResource->GetObject("Ground")->setScale(30.0f,1.0f);
	levelResource->GetObject("Ground")->setMass(0.0f);
	levelResource->GetObject("Ground")->setFriction(10.0f);
	levelResource->GetObject("Ground")->setElastic(0.0f);
	levelResource->GetObject("Ground")->createPhysics(BOX, STATIC);

	levelResource->GetObject("Ceiling")->setPosition(0.0f,30.0f,69.0f);
	levelResource->GetObject("Ceiling")->setScale(30.0f,1.0f);
	levelResource->GetObject("Ceiling")->setMass(0.0f);
	levelResource->GetObject("Ceiling")->setFriction(0.5f);
	levelResource->GetObject("Ceiling")->setElastic(0.0f);
	levelResource->GetObject("Ceiling")->createPhysics(BOX, STATIC);

	levelResource->GetObject("L_Wall")->setPosition(-29.0f,0.0f,69.0f);
	levelResource->GetObject("L_Wall")->setScale(1.0f,30.0f);
	levelResource->GetObject("L_Wall")->setMass(0.0f);
	levelResource->GetObject("L_Wall")->setFriction(0.5f);
	levelResource->GetObject("L_Wall")->setElastic(0.0f);
	//levelResource->GetObject("L_Wall")->createPhysics(BOX, STATIC);

	levelResource->GetObject("R_Wall")->setPosition(29.0f,0.0f,69.0f);
	levelResource->GetObject("R_Wall")->setScale(1.0f,30.0f);
	levelResource->GetObject("R_Wall")->setMass(0.0f);
	levelResource->GetObject("R_Wall")->setFriction(0.5f);
	levelResource->GetObject("R_Wall")->setElastic(0.0f);
	//levelResource->GetObject("R_Wall")->createPhysics(BOX, STATIC);

	levelResource->GetObject("Platform_1")->setPosition(7.1f,-14.0f,69.0f);
	levelResource->GetObject("Platform_1")->setScale(5.0f,0.5f);
	levelResource->GetObject("Platform_1")->setMass(0.0f);
	levelResource->GetObject("Platform_1")->setFriction(0.5f);
	levelResource->GetObject("Platform_1")->setElastic(0.0f);
	levelResource->GetObject("Platform_1")->createPhysics(BOX, STATIC);

	levelResource->GetObject("Platform_2")->setPosition(-15.0f,-12.0f,69.0f);
	levelResource->GetObject("Platform_2")->setScale(4.0f,0.8f);
	levelResource->GetObject("Platform_2")->setMass(0.0f);
	levelResource->GetObject("Platform_2")->setFriction(0.5f);
	levelResource->GetObject("Platform_2")->setElastic(0.6f);
	levelResource->GetObject("Platform_2")->createPhysics(BOX, STATIC);

	levelResource->GetObject("Platform_3")->setPosition(-10.0f,0.0f,69.0f);
	levelResource->GetObject("Platform_3")->setScale(4.0f,0.5f);
	levelResource->GetObject("Platform_3")->setMass(0.0f);
	levelResource->GetObject("Platform_3")->setFriction(0.5f);
	levelResource->GetObject("Platform_3")->setElastic(0.6f);
	levelResource->GetObject("Platform_3")->createPhysics(BOX, STATIC);

}

void LevelTest::AddMainCharacter(Entity * mainCharacter)
{
	levelResource->AddObject(mainCharacter);
}

void LevelTest::Render()
{
	/**
	Render all objects in level.

	Parameter 1 -> Camera * camera
		--Pass a ptr from an instanced camera so that the cameraMatrix is applied to all rendered objects
	*/
	levelResource->Render(this->camera);
}

void LevelTest::SetZoom(float zoom)
{

	/**
	Lets us easily zoom the camera in and out.
	*/

	this->camera->Zoom(zoom);
}

b2World * LevelTest::GetWorldPhysics()
{
	return world_physics;
}